package com.ws.sms; 

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Format {

	/**
	 * string类型当前日期
	 * @return
	 */
	public static String formatDate(){
		String sdate=time();
		return sdate.substring(0,8);
	}
	/**
	 * String类型当前时间6位
	 * @return
	 */
	public static String formatTime(){
		String sdate=time();
		return sdate.substring(8,14);
	}
	/**
	 * String类型当前时间14位
	 * @return
	 */
	public static String time(){ 
		Date date=new Date();
		SimpleDateFormat sd=new SimpleDateFormat("yyyyMMddHHmmss");
		String sdate=sd.format(date);
		return sdate;
	}
	/**
	 * String类型 6位随机码
	 * @return
	 */
	public static String getRandom(){		
		StringBuffer random=new StringBuffer();
		Random r=new Random(); 
		for(int i=0; i<6; i++){
			random.append(r.nextInt(9));
		}
		return random.toString();
	}
	/**
	 * String类型 8位随机码
	 * @return
	 */
	public static String getRandom8(){		
		StringBuffer random=new StringBuffer();
		Random r=new Random(); 
		for(int i=0; i<8; i++){
			random.append(r.nextInt(9));
		}
		return random.toString();
	}
	/**
	 * String 元转分
	 * @param amount
	 * @return
	 */
	public static String ytf(String amount){
		
		DecimalFormat df = new DecimalFormat("0");
		double temp = Double.valueOf(amount);
		temp = temp*100;
		return df.format(temp);		
	}
	
	/**
	 * String 分转元
	 * @param amount
	 * @return
	 */
	public static String fty(String amount){
		
		DecimalFormat df = new DecimalFormat("0.00");
		double temp = Double.valueOf(amount);
		temp = temp/100;
		return df.format(temp);		
	}
	
	/**
	 * Long 分转元
	 * @param amount
	 * @return
	 */
	public static Long fty(Long amount){
		
		amount = amount/100;
		return Long.valueOf(String.valueOf(amount));		
	}
	/**
	 * 左填0
	 * 
	 * @param s
	 * @param length
	 * @return
	 */
	public static String addLeftZero(String s, int length) {
		// StringBuilder sb=new StringBuilder();
		int old = s.length();
		if (length > old) {
			char[] c = new char[length];
			char[] x = s.toCharArray();
			if (x.length > length) {
				throw new IllegalArgumentException(
						"Numeric value is larger than intended length: " + s
								+ " LEN " + length);
			}
			int lim = c.length - x.length;
			for (int i = 0; i < lim; i++) {
				c[i] = '0';
			}
			System.arraycopy(x, 0, c, lim, x.length);
			return new String(c);
		}
		return s.substring(0, length);

	}
	
	public static void main(String[] args) throws ParseException{
		System.out.println(Format.fty(101234l));
		String adt="2012:12:21 15:58:00";
		SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
		Date d=sdf.parse(adt.substring(adt.length()-8,adt.length()));
		System.out.println(d.getTime());
	}
}
